package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private DishService dishService;
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户ID，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.GetCurrentId();
        shoppingCart.setUserId(currentId);
        BigDecimal amount = shoppingCart.getAmount().multiply(BigDecimal.valueOf(100));

        //查询当前菜品或套餐是否在购物车内
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }
        Dish byId1 = dishService.getById(dishId);
        BigDecimal price = byId1.getPrice();
        if(price.compareTo(amount) != 0){
            log.info("拍品更新了");
            log.info(amount.toString());
            log.info(price.toString());
            throw new CustomException("拍品价格更新了");
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);


        //若存在，则在原来基础上加一
        if(cartServiceOne != null){
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);

            shoppingCartService.updateById(cartServiceOne);
        }else{
            //不存在则添加到购物车，默认数量为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            Long id = shoppingCart.getDishId();
            Dish byId = dishService.getById(id);

            shoppingCart.setDnumber(byId.getNumber());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;

        }
        return R.success(cartServiceOne);




    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //设置用户ID，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.GetCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前菜品或套餐是否在购物车内
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);


        //若存在，则在原来基础上减一
        if(cartServiceOne != null){
            Integer number = cartServiceOne.getNumber() - 1;
            //数量为0，则清除数据库数据
            if(number == 0){
                shoppingCartService.removeById(cartServiceOne);
                cartServiceOne.setNumber(number);
                return R.success(cartServiceOne);

            }else{
                cartServiceOne.setNumber(number);
                shoppingCartService.updateById(cartServiceOne);

            }


        }else{
            return R.error("禁止减少");
        }

        return R.success(cartServiceOne);




    }


    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>>list(){

        LambdaQueryWrapper<ShoppingCart>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.GetCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);


    }
    @DeleteMapping("/clean")
    public R<String>clean(){

        LambdaQueryWrapper<ShoppingCart>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.GetCurrentId());
        shoppingCartService.remove(queryWrapper);

        return R.success("清空购物车成功");

    }
}
